var app = getApp();
var wxCharts = require("../../../../utils/wxcharts.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    month: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
    year: "",
    // 本月钱
    y_zhou: [],
    // 本月天
    x_zhou: [],
    // 本年几个月的 钱
    y_zhou_y: [],
    // 本年十二个月
    x_zhou_y: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
    num: 0,
    money: 50.00,
    tian: ""
  },

  bindYear(e) {
    var that = this;
    that.setData({
      year: e.detail.value
    })
    console.log(that.data.year);
    that.getCaiwus(that.data.year);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var date = new Date();
    var month = that.data.month
    var zid = options.zid
    var months = date.getMonth() + 1;
    var num = '';
    for (var i in month) {
      if (parseInt(month[i]) == months) {
        num = i;
        that.setData({
          index: num
        })
      }
    }

    that.setData({
      year: date.getFullYear(),
      zid: zid
    })
    that.getCaiwu();
    that.getCaiwus(that.data.year);
  },
  zhexiantu(id, color, x_zhou, y_zhou) {
    //console.log(x_zhou, y_zhou);
    var that = this;
    //折线图开始
    try {
      var res = wx.getSystemInfoSync();
      var windowWidth = res.windowWidth;

      var windowHeight = res.windowHeight * 0.4;
    } catch (e) {
      console.error("系统信息错误")
    };
    new wxCharts({
      canvasId: id,
      type: "line",
      categories: x_zhou,
      animation: true,
      series: [{
        data: y_zhou,
        format: function(val, name) {
          return val + ' 元';
        },
        color: color
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        // disabled: true
      },
      width: windowWidth,
      height: windowHeight,
      dataLabel: true,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      },
      legend: false
    })


  },


  //近七天、今日收益
  getCaiwu: function() {
    var that = this;
    var zid = that.data.zid;
    app.util.request({
      'url': 'entry/wxapp/Cweekmoney',
      'data': {
        zid: zid,
        op: 'zhou'
      },
      success: function(res) {
        // console.log(res.data);
        // var data = JSON.stringify(res.data);
        var arrs = res.data;
        that.setData({
          tian: res.data
        })

        var obj = {
          news: {
            money: 0,
            num: 0
          },
          tian: arrs
        };
        var x_zhou = [],
          y_zhou = [];

        console.log(obj.tian);
        for (var i in obj.tian) {
          x_zhou.push(obj.tian[i].time);
          y_zhou.push(parseInt(obj.tian[i].money));
        }
        // var x_zhou = ["2015-02-20", "2015-02-20", "2015-02-20", "2015-02-20", "2015-02-20"],
        //   y_zhou = [2, 2, 2, 2, 2];
        that.setData({
          x_zhou: x_zhou,
          y_zhou: y_zhou,
          num: obj.news.num,
          money: obj.news.money
        })
        that.zhexiantu("lineCanvas", "#F2AA56", that.data.x_zhou, that.data.y_zhou);
        //console.log(that.data.tian);
      }
    })
  },

  // 年收益
  getCaiwus: function(year) {
    var that = this;
    var zid = 7;
    app.util.request({
      'url': 'entry/wxapp/Cweekmoney',
      'data': {
        zid: zid,
        op: 'nian',
        year: year
      },
      success: function(res) {
        console.log(res);
        var arrs = res.data;
        var x_zhou_y = [],
          y_zhou_y = [];

        for (var i in arrs) {
          x_zhou_y.push(arrs[i].time);
          y_zhou_y.push(parseInt(arrs[i].money));
        }
        if (arrs[0].summoney == undefined) {
          var sum = 0
        } else {
          var sum = arrs[0].summoney[0].sum
        }
        if (arrs[0].num == undefined) {
          var num = 0
        } else {
          var num = arrs[0].num[0].num
        }
        // var x_zhou_y = ["2015-02-20", "2015-02-20", "2015-02-20", "2015-02-20", "2015-02-20"],
        //   y_zhou_y = [0, 0, 0, 0, 0];
        that.setData({
          x_zhou_y: x_zhou_y,
          y_zhou_y: y_zhou_y,
          money: sum,
          num: num,
        })
        that.zhexiantu("lineCanvas2", "#6D5EFA", that.data.x_zhou_y, that.data.y_zhou_y);
      }
    })
  },

})